package com.zkdcloud.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xdja.interceptor.bean.ResultBean;
import com.zkdcloud.domain.Admin;
import com.zkdcloud.domain.User;
import com.zkdcloud.dto.Token;
import com.zkdcloud.service.AdminService;
import com.zkdcloud.service.UserService;
import com.zkdcloud.util.ResponseAndroidUtil;
import com.zkdcloud.util.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.HTTP_SUCCESS;

/**
 * 登录controller
 *
 * @author zk
 * @since 2018/4/4
 */
@RestController
@RequestMapping(value = "/api")
public class MobileLoginController {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(MobileLoginController.class);

    @Autowired
    private UserService userService;
    /**
     * 登录controller
     *
     * @return bean
     */
    @RequestMapping(value = "/token/access",method = RequestMethod.POST)
    public Object accessToken(User user, HttpServletResponse response){
        Token token = new Token();
        User getedUser = userService.getUserByPhotoNumAndPasswordWithEncodePwd(user.getUsername(),user.getPassword());
        if(getedUser != null){
            token.setUser_id(getedUser.getId());
            token.setAccess_token(TokenUtil.makeUserToken(getedUser));
        } else {
            ResponseAndroidUtil.responseError("账号或密码错误",response);
        }
        return token;
    }
}
